package com.xujl.fastlib.utils;

import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.util.Log;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ResUtils {
    public static float getDimen (int id) {
        return ConfigManager.getInstance().getContext().getResources().getDimension(id);
    }

    public static int getColor (int id) {
        return ConfigManager.getInstance().getContext().getResources().getColor(id);
    }

    public static Drawable getDrawable (int id) {
        return ConfigManager.getInstance().getContext().getResources().getDrawable(id);
    }

    public static String getString (int id) {
        return ConfigManager.getInstance().getContext().getResources().getString(id);
    }

    /**
     * 匹配文字变色
     *
     * @param color
     * @param text
     * @param keyword
     * @return
     */
    public static SpannableString matcherSearchText (int color, String text, String keyword) {
        SpannableString ss = new SpannableString(text);
        Pattern pattern = Pattern.compile(keyword);
        Matcher matcher = pattern.matcher(ss);
        while (matcher.find()) {
            int start = matcher.start();
            int end = matcher.end();
            ss.setSpan(new ForegroundColorSpan(color), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        return ss;
    }

    public static int getLayoutResForString (String layoutStr) {
        return ConfigManager.getInstance().getContext().getResources().getIdentifier(layoutStr, "layout",
                ConfigManager.getInstance().getContext().getPackageName());
    }

    /**
     * 获取当前app version code
     */
    public static long getAppVersionCode () {
        long appVersionCode = 0;
        try {
            PackageInfo packageInfo = ConfigManager.getInstance().getContext().getApplicationContext()
                    .getPackageManager()
                    .getPackageInfo(ConfigManager.getInstance().getContext().getPackageName(), 0);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
                appVersionCode = packageInfo.getLongVersionCode();
            } else {
                appVersionCode = packageInfo.versionCode;
            }
        } catch (PackageManager.NameNotFoundException e) {
            Log.e("", e.getMessage());
        }
        return appVersionCode;
    }

    /**
     * 获取当前app version name
     */
    public static String getAppVersionName () {
        String appVersionName = "";
        try {
            PackageInfo packageInfo = ConfigManager.getInstance().getContext().getApplicationContext()
                    .getPackageManager()
                    .getPackageInfo(ConfigManager.getInstance().getContext().getPackageName(), 0);
            appVersionName = packageInfo.versionName;
        } catch (PackageManager.NameNotFoundException e) {
            Log.e("", e.getMessage());
        }
        return appVersionName;
    }
}
